package cn.sxt.logistics.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.sxt.logistics.pojo.MsgObject;
import cn.sxt.logistics.pojo.BaseData;
import cn.sxt.logistics.pojo.Customer;
import cn.sxt.logistics.pojo.CustomerExample;
import cn.sxt.logistics.pojo.CustomerExample.Criteria;
import cn.sxt.logistics.pojo.User;
import cn.sxt.logistics.pojo.UserExample;
import cn.sxt.logistics.service.BaseDataService;
import cn.sxt.logistics.service.CustomerService;
import cn.sxt.logistics.service.UserService;
import cn.sxt.logistics.util.Constant;

@Controller
@RequestMapping("/customer")
public class CustomerController {

	@Autowired
	private CustomerService customerService;
	
	
	@Autowired
	private UserService userService;
	
	
	@Autowired
	private BaseDataService baseDataService;

	@RequestMapping("/customerPage")
	@RequiresPermissions("customer:customerPage")
	public String customerPage() {

		return "customerPage";
	}

	// 编辑页面，新增和修改公用的页面
	@RequestMapping("/edit")
	public String edit(Model m, Long customerId) {
		
		UserExample example = new UserExample();
		cn.sxt.logistics.pojo.UserExample.Criteria criteria = example.createCriteria();
		criteria.andRolenameEqualTo(Constant.ROLE_SALESMAN);
		//查询所有角色名称为业务员的管理员
		List<User> users = userService.selectByExample(example );
		m.addAttribute("users", users);
		
		
		//查询所有的区间数据
		List<BaseData> intervals = baseDataService.selectBaseDatasByParentName("区间管理");
		m.addAttribute("intervals", intervals);

		// 修改操作
		if (customerId != null) {
			// 查询出当前用户的数据，共享给编辑页面，以供修改的回显数据
			Customer customer = customerService.selectByPrimaryKey(customerId);
			m.addAttribute("customer", customer);
		}

		return "customerEdit";
	}
	
	@RequiresPermissions("customer:list")
	@RequestMapping("/list")
	@ResponseBody
	public PageInfo<Customer> list(@RequestParam(defaultValue = "1") Integer pageNum,
			@RequestParam(defaultValue = "10") Integer pageSize, String keyword) {
		System.out.println("keyword :" + keyword);
		// 开始分页
		PageHelper.startPage(pageNum, pageSize);

		CustomerExample example = new CustomerExample();
		Criteria criteria1 = example.createCriteria();
		//获取当前身份。判断当前是否是业务员，如果是业务员，就只能查看自己的客户
		Subject subject = SecurityUtils.getSubject();
		User user = (User) subject.getPrincipal();
		System.out.println("user :"+user);
		
		if(user.getRolename().equals(Constant.ROLE_SALESMAN)) {
			//查询当前业务员的客户
			criteria1.andRealnameEqualTo(user.getRealname());
		}

		
		// 关键字不为空才做条件查询
		if (StringUtils.isNotBlank(keyword)) {
			// 创建限制对象
			
			criteria1.andCustomerNameLike("%" + keyword + "%");

		}

		List<Customer> customers = customerService.selectByExample(example);

		// 创建分页信息对象
		return new PageInfo<Customer>(customers);

	}

	// 删除功能
	@RequestMapping("/insert")
	@ResponseBody
	public MsgObject insert(Customer customer) {

		int row = customerService.insert(customer);
		MsgObject mo = MsgObject.createMO(0, "新增数据失败，请联系管理员！！");
		if (row == 1) {
			mo = MsgObject.createMO(1, "新增数据成功!");
		}
		return mo;
	}

	// 删除功能
	@RequestMapping("/update")
	@ResponseBody
	public MsgObject update(Customer customer) {

		int row = customerService.updateByPrimaryKeySelective(customer);
		MsgObject mo = MsgObject.createMO(0, "修改数据失败，请联系管理员！！");
		if (row == 1) {
			mo = MsgObject.createMO(1, "修改数据成功!");
		}
		return mo;
	}

	// 删除功能
	@RequestMapping("/delete")
	@ResponseBody
	public MsgObject delete(Long customerId) {
		
		
		MsgObject mo = MsgObject.createMO(0, "删除数据失败，请联系管理员！！");
		


		int row = customerService.deleteByPrimaryKey(customerId);
		if (row == 1) {
			mo = MsgObject.createMO(1, "删除数据成功!");
		}
		return mo;
	}

}
